package com.ebookstore.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ebookstore.exception.EBookStoreException;

public class GlobalAction extends BaseAction {
	//定义常量
	public static final String GLOBAL_BOOK = "global_book_list";//图书列表
	public static final String GLOBAL_CATEGORY = "global_category_list";//类别列表
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws EBookStoreException {
		//在Session中查找是否存在图书分类
		List category = (List) request.getSession().getAttribute(
				GLOBAL_CATEGORY);
		//如果分类不存在，则读取分类信息并放入到Session中
		if (category == null) {
			//调用业务逻辑
			category = getEBookStore().getBookService().findCategory();
			request.getSession().setAttribute(GLOBAL_CATEGORY, category);
		}
		//在Session中查找是否存在图书
		List books = (List) request.getSession().getAttribute(GLOBAL_BOOK);
		//如果图书不存在，则读取图书信息并放入到Session中
		if (books == null) {
			//调用业务逻辑
			books = getEBookStore().getBookService().findBookByPublishDate();
			request.getSession().setAttribute(GLOBAL_BOOK, books);
		}
		return mapping.findForward("index");
	}
}